package com.etung.iot.demo.controller;

import com.etung.iot.demo.core.Authorized;
import com.etung.iot.demo.modles.BoxConfig;
import com.etung.iot.demo.modles.ProjectEntity;
import com.etung.iot.demo.vo.req.*;
import com.etung.iot.demo.boxConfig.Box;
import com.etung.iot.demo.vo.resp.DeviceAccessInfo;
import com.etung.iot.demo.services.AccessService;
import com.etung.iot.demo.services.BoxConfigService;
import com.etung.iot.demo.services.VariantMgrService;
import com.etung.iot.demo.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/demo")
@Api(tags = "Demo接口")
public class DemoController {
    @Autowired
    private BoxConfigService boxConfigService;
    @Autowired
    private AccessService accessService;
    @Autowired
    private VariantMgrService variantMgrService;


    @GetMapping("/test")
    public Result<?> test(){
        return Result.OK("hello world");
    }

    @Authorized
    @PostMapping("/pushConfig")
    @ApiOperation(value = "推送BOX配置",response = Result.class)
    public Result <?> pushConfig(@RequestBody Box box){
        boxConfigService.saveConfig(box);
        return Result.OK();
    }

    @Authorized
    @PostMapping("/genAccessInfo")
    @ApiOperation(value = "生成BOX访问信息")
    public Result <?> genAccessInfo(@RequestBody AccessRequest accessRequest){
        return Result.OK(accessService.genAccessInfo(accessRequest));
    }
    @Authorized
    @PostMapping("/variantControl")
    @ApiOperation(value = "控制变量")
    public Result <?> variantControl(@RequestBody VariantControl variant){
        variantMgrService.controlVariant(variant);
        return Result.OK();
    }

    @Authorized
    @PostMapping("/variantDataList")
    @ApiOperation(value = "获取变量列表")
    public Result < ? > variantDataList(@RequestBody VariantQuery variantQuery){
        return Result.OK(variantMgrService.listData(variantQuery));
    }

    @Authorized
    @PostMapping("/saveConfig")
    @ApiOperation(value = "保存BOX配置")
    public Result <?> saveConfig(@RequestBody BoxConfigReq boxConfigReq){
        boxConfigService.saveFile(boxConfigReq);
        return Result.OK();
    }

    @Authorized
    @PostMapping("/selectConfig")
    @ApiOperation(value = "查询BOX配置")
    public Result <?> selectConfig(@RequestBody ProjectReq project){
        String configSetting = boxConfigService.selectByUser(project);
        return Result.OK(configSetting);
    }


}
